package com.thon.service.project.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.thon.dao.project.ProjectBudgetDao;
import com.thon.entity.project.ProjectBudget;
import com.thon.entity.project.ProjectSchedule;
import com.thon.service.project.ProjectBudgetService;

/**
 * @file ProjectBudgetServiceImpl.java
 * @author Zemo
 * @mail zemochen@gmail.com
 * @data 2013年11月12日 下午6:05:22
 * @description: TODO
 */
@Service
@Transactional
public class ProjectBudgetServiceImpl implements ProjectBudgetService {
	
	@Autowired
	private ProjectBudgetDao projectBudgetDao;

	@Override
	public void addBudget(ProjectBudget projectBudget) {
		
		projectBudgetDao.save(projectBudget);
		
	}
	
	@Override
	public void updateBudget(ProjectBudget projectBudget) {
		projectBudgetDao.update(projectBudget);
	}
	
	@Override
	public ProjectBudget getBudget(int id) {
		
		ProjectBudget projectBudget = projectBudgetDao.get(id);
	
		
		return projectBudget;
	}

	@Override
	public List<ProjectBudget> listProjectBudgets(int projectId) {
		
		List<ProjectBudget> projectBudgets = projectBudgetDao.listProjectBudgets(projectId);
		return projectBudgets;
	}

	@Override
	public ProjectBudget getProjectBudget(Integer stage, Integer projectId) {
		return projectBudgetDao.getProjectBudget(stage, projectId);
	}

	@Override
	public void deleteBudget(Integer budgetId) {
		
		ProjectBudget projectBudget = projectBudgetDao.get(budgetId);
		Integer pid = projectBudget.getProject().getId();
		projectBudgetDao.delete(budgetId);
		List<ProjectBudget> projectBudgets = projectBudgetDao.listProjectBudgets(pid);
		for (int i = 0; i < projectBudgets.size(); i++) {
			ProjectBudget pb = projectBudgets.get(i);
			pb.setStage(i+1);
			projectBudgetDao.update(pb);
		}
		
	}
}
